﻿$(function () {
    //laydate.render({ elem: '#txtDate', type: 'date' });
    BindLayout();
    Search();
    $('#btnSearch').click(function () { Search(); });
})
//Layout布局样式调整
function BindLayout() {
    var h = document.documentElement.clientHeight;
    var w = document.documentElement.clientWidth;
    $("#layout").ligerLayout({ rightWidth: w * 0.5, allowRightCollapse: false });
    $('#divTimeline').css('height', h - 90)
}
var detailbtn = null;
//获取患者数据
function GetPatientInfo(queryData, page) {
    $("#divPatientInfo").find("tbody").children().remove();
    $("#divPatientInfo").ligerGrid({
        height: '100%',
        url: '/PatientList/Search?ram=' + new Date().getMilliseconds(),
        parms: page,
        page: 1,
        pageSize: 20,
        rownumbers: false,
        heightDiff: -20,
        isScroll: true,
        columns: [
            { display: '就诊ID', name: 'PATIENT_ID', minWidth: 50, width: 120, },
            { display: '姓名', name: 'PATIENT_NAME', minWidth: 100, width: 100 },
            {
                display: '性别', name: 'PATIENT_SEX', minWidth: 50, width: 60,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_SEX) == "undefined" || rowData.PATIENT_SEX == null || rowData.PATIENT_SEX == "-1")
                        return "未知";
                    if (rowData.PATIENT_SEX == "0")
                        return "男";
                    if (rowData.PATIENT_SEX == "1")
                        return "女";
                    return "<span title=\"" + rowData.PATIENT_SEX + "\">" + rowData.PATIENT_SEX + "</span>";
                }
            },
            { display: '年龄', name: 'PATIENT_AGE', minWidth: 50, width: 60, },
            {
                display: '腕带状态', name: 'RFID_State', minWidth: 100, width: 60,
                render: function (rowData) {
                    if (typeof (rowData.RFID_State) == "undefined" || rowData.RFID_State == null)
                        return "未知";
                    if (rowData.RFID_State == "0")
                        return "未绑定";
                    if (rowData.RFID_State == "1")
                        return "<span style=\"color:green\">已绑定</span>";
                    return "<span title=\"" + rowData.RFID_State + "\">未知</span>";
                }
            },
            {
                display: '患者状态', name: 'PATIENT_STATUS', minWidth: 50, width: 60,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_STATUS) == "undefined" || rowData.PATIENT_STATUS == null)
                        return "未知";
                    if (rowData.PATIENT_STATUS == "0")
                        return "院前";
                    if (rowData.PATIENT_STATUS == "1")
                        return "在科";
                    return "<span title=\"" + rowData.PATIENT_STATUS + "\">未知</span>";
                }
            },
            {
                display: '绿色通道', name: 'IsGreenChannel', minWidth: 50, width: 60,
                render: function (rowData) {
                    if (rowData.IsGreenChannel == "1")
                        return "<span style=\"color:green\">已启动</span>";
                    else
                        return "未启动";
                }
            },
            {
                display: '到达医院时间', name: 'EMS_ARRIVEDOOR_TIME', minWidth: 150, width: 150,
                render: function (rowData) {
                    var time = rowData.EMS_ARRIVEDOOR_TIME;
                    return obj2Timemm(time);
                }
            },
            {
                display: '来院方式', name: 'PATIENT_ARRIVE_CLASS', minWidth: 100,
                render: function (rowData) {
                    if (typeof (rowData.PATIENT_ARRIVE_CLASS) == "undefined" || rowData.PATIENT_ARRIVE_CLASS == null || rowData.PATIENT_ARRIVE_CLASS == "0")
                        return "未知";
                    if (rowData.PATIENT_ARRIVE_CLASS == "1")
                        return "120";
                    if (rowData.PATIENT_ARRIVE_CLASS == "2")
                        return "转院";
                    if (rowData.PATIENT_ARRIVE_CLASS == "3")
                        return "自行来院";
                    if (rowData.PATIENT_ARRIVE_CLASS == "4")
                        return "院内发病";
                    return "<span title=\"" + rowData.PATIENT_ARRIVE_CLASS + "\">不祥</span>";
                }
            }
        ],
        onSuccess: function (data, grid) {
            $("#spanPatientNum").text(data.Total);
            $("#spanInHospitalNum").text(data.InHospitalNum);
            $("#spanPreHospitalNum").text(data.PreHospitalNum);
        },
        detail: { onShowDetail: GetDetail, height:50 },
        onSelectRow: function (data, rowindex, rowobj) {
            BindTimeline(data.PATIENT_ID);
            var asd = $(rowobj.id).find("span");
        }
    });
}
// 展示明细
function GetDetail(row, detailPanel) {
    var detailhtml = '<div style="margin:10px; font-size:14px;">';
    detailhtml += '<span style="font-weight:bolder;">初步诊断:</span>' + (typeof (row.DiagnosticPathName) == "undefined" || row.DiagnosticPathName == null || row.DiagnosticPathName == "" ? '未诊断' : row.DiagnosticPathName);
    detailhtml += '</div>';
    $(detailPanel).append(detailhtml);
}
// 查询按钮
function Search() {
    var queryData = {
        name: $("#txtName").val(),
        status: $("#hdnstatus").val(),
        pathway: $("#hdnpathway").val(),
        cometype: $("#hdncomeType").val()
    }
    GetPatientInfo(queryData, 1);
}
//获取时间轴
function BindTimeline(patientId) {
    $.post("/ChestPainCenter/PatientTrajectory/GetPatientTrajectory", {
        rnd: new Date().getMilliseconds(),
        patientId: patientId
    }, function (result) {
        var json = [];
        $.each(eval(result), function (i, a) {
            json.push({ date: a.EventTime, title: a.PositionName });
        })
        if (json != []) {
            var option = {
                id: '#divTimeline',
                data: json,
                order: 'asc',
                onSuccess: function () { },
                onError: function () { },
                onSelect: function (i, data) { }
            }
            Timeline.buildTimeline(option)
        }
    })
}